using System.Diagnostics;

namespace StudentVideo.Middleware;

public class RequestLoggingMiddleware(RequestDelegate next, ILogger logger) {
   private readonly RequestDelegate _next = next;
   private readonly ILogger _logger = logger;

   public async Task InvokeAsync(HttpContext context) {
      var stopwatch = Stopwatch.StartNew();

      _logger.LogInformation("Request: {Method} {Path}", context.Request.Method, context.Request.Path);

      await _next(context);

      stopwatch.Stop();

      _logger.LogInformation("Response: {StatusCode} in {ElapsedMilliseconds}ms", context.Response.StatusCode, stopwatch.ElapsedMilliseconds);
   }
}
